home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Xconq 7.0d37 / source / kernel / generic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  7.8 KB  |  317 lines  |  [TEXT/KAHL]

  1. /* Low-level support for variables and properties in Xconq GDL.
  2.    Copyright (C) 1991, 1992, 1993, 1994 Stanley T. Shebs.
  3.  
  4. Xconq is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.  See the file COPYING.  */
  8.  
  9. #include "config.h"
  10. #include "misc.h"
  11. #include "lisp.h"
  12. #include "game.h"
  13.  
  14. /* The total number of unit/material/terrain types. */
  15.  
  16. short numutypes;
  17. short nummtypes;
  18. short numttypes;
  19.  
  20. /* Doublechecks on type indices.  These can show up all kinds of little
  21.    glitches. */
  22.  
  23. /* It would be useful for these to return the slot or table access that
  24.    resulted in the error, but we don't want to have to pass a bunch of
  25.    parameters to these routines, since they're being called a lot. */
  26.  
  27. void
  28. checku(x)
  29. int x;
  30. {
  31.     if ((x) < 0 || (x) >= numutypes) utype_error(x);
  32. }
  33.  
  34. void
  35. utype_error(u)
  36. int u;
  37. {
  38.     run_warning("Bad utype %d", u);
  39. }
  40.  
  41. void
  42. checkm(x)
  43. int x;
  44. {
  45.     if ((x) < 0 || (x) >= nummtypes) mtype_error(x);
  46. }
  47.  
  48. void
  49. mtype_error(r)
  50. int r;
  51. {
  52.     run_warning("Bad mtype %d", r);
  53. }
  54.  
  55. void
  56. checkt(x)
  57. int x;
  58. {
  59.     if ((x) < 0 || (x) >= numttypes) ttype_error(x);
  60. }
  61.  
  62. void
  63. ttype_error(t)
  64. int t;
  65. {
  66.     run_warning("Bad ttype %d", t);
  67. }
  68.  
  69. #ifdef SPECIAL
  70.  
  71. /* When specially compiled, all the types and globals are statically
  72.    initialized. */
  73.  
  74. void
  75. init_types()
  76. {
  77. }
  78.  
  79. void
  80. init_globals()
  81. {
  82. }
  83.  
  84. #else /* SPECIAL */
  85.  
  86. /* Declarations of the type definitions themselves. */
  87.  
  88. Utype *utypes;
  89.  
  90. Mtype *mtypes;
  91.  
  92. Ttype *ttypes;
  93.  
  94. Globals globals;
  95.  
  96. /* This prepares the type definitions to be filled in, doing initial
  97.    allocations, etc. */
  98.  
  99. int curmaxutypes = MAXUTYPES;
  100. int curmaxmtypes = MAXMTYPES;
  101. int curmaxttypes = MAXTTYPES;
  102.  
  103. void
  104. init_types()
  105. {
  106.     numutypes = nummtypes = numttypes = 0;
  107.  
  108.     Dprintf("Utype is %d bytes, mtype is %d bytes, ttype is %d bytes.\n",
  109.         sizeof(Utype), sizeof(Mtype), sizeof(Ttype));
  110.  
  111.     utypes = (Utype *) xmalloc(sizeof(Utype) * curmaxutypes);
  112.     mtypes = (Mtype *) xmalloc(sizeof(Mtype) * curmaxmtypes);
  113.     ttypes = (Ttype *) xmalloc(sizeof(Ttype) * curmaxttypes);
  114.  
  115.     memset(utypes, 0, sizeof(Utype) * curmaxutypes);
  116.     memset(mtypes, 0, sizeof(Mtype) * curmaxmtypes);
  117.     memset(ttypes, 0, sizeof(Ttype) * curmaxttypes);
  118. }
  119.  
  120. VarDefn vardefns[] = {
  121.  
  122. #undef  DEF_VAR_I
  123. #define DEF_VAR_I(NAME,FNAME,setfname,DOC,var,LO,DFLT,HI)  \
  124.     { NAME, FNAME, NULL, NULL, DOC, DFLT, NULL, NULL, LO, HI },
  125. #undef  DEF_VAR_S
  126. #define DEF_VAR_S(NAME,FNAME,setfname,DOC,var,DFLT)  \
  127.     { NAME, NULL, FNAME, NULL, DOC,    0, DFLT, NULL,  0,  0 },
  128. #undef  DEF_VAR_L
  129. #define DEF_VAR_L(NAME,FNAME,setfname,DOC,var,DFLT)  \
  130.     { NAME, NULL, NULL, FNAME, DOC,    0, NULL, DFLT,  0,  0 },
  131.  
  132. #include "gvar.def"
  133.  
  134.     { NULL }
  135. };
  136.  
  137. /* Define all the global-getting and -setting functions. */
  138.  
  139. #undef  DEF_VAR_I
  140. #define DEF_VAR_I(str,FNAME,SETFNAME,doc,VAR,lo,dflt,hi)  \
  141.   int FNAME() { return globals.VAR; }  \
  142.   void SETFNAME(v) int v; { globals.VAR = v; }
  143. #undef  DEF_VAR_S
  144. #define DEF_VAR_S(str,FNAME,SETFNAME,doc,VAR,dflt)  \
  145.   char *FNAME() { return globals.VAR; }  \
  146.   void SETFNAME(v) char *v; { globals.VAR = v; }
  147. #undef  DEF_VAR_L
  148. #define DEF_VAR_L(str,FNAME,SETFNAME,doc,VAR,DFLT)  \
  149.   Obj *FNAME() {  \
  150.       void (*fn) PROTO ((void)) = (DFLT);  \
  151.       if (fn != NULL && globals.VAR == NULL) (*fn)();  \
  152.       return globals.VAR;  \
  153.   }  \
  154.   void SETFNAME(v) Obj *v; { globals.VAR = v; }
  155.  
  156. #include "gvar.def"
  157.  
  158. /* Set the globals to their default values. */
  159.  
  160. void
  161. init_globals()
  162. {
  163.  
  164.     /* (should zero out all the globals, only set the nonzero defaults) */
  165.  
  166. #undef  DEF_VAR_I
  167. #define DEF_VAR_I(str,fname,SETFNAME,doc,var,lo,DFLT,hi)  \
  168.     if ((DFLT) != 0 || 1 /* for now */) SETFNAME(DFLT);
  169. #undef  DEF_VAR_S
  170. #define DEF_VAR_S(str,fname,SETFNAME,doc,var,DFLT)  \
  171.     if ((DFLT) != NULL || 1 /* for now */) SETFNAME(DFLT);
  172. #undef  DEF_VAR_L
  173. #define DEF_VAR_L(str,fname,SETFNAME,doc,var,DFLT)  \
  174.     if ((DFLT) == NULL) SETFNAME(lispnil);
  175.  
  176. #include "gvar.def"
  177.  
  178. }
  179.  
  180. #define TYPEPROP(TYPES, N, DEFNS, I, TYPE)  \
  181.   ((TYPE *) &(((char *) (&(TYPES[N])))[DEFNS[I].offset]))[0]
  182.  
  183. /* MPW C always loses on multiply-nested array references, so expand them
  184.    into pointer-arithmetic equivalents, which seem to work fine. */
  185.  
  186. #ifdef MPW_C
  187. #undef TYPEPROP
  188. #define TYPEPROP(TYPES, N, DEFNS, I, TYPE)  \
  189.   ((TYPE *) &(((char *) (&(TYPES[N])))[(*((DEFNS)+(I))).offset]))[0]
  190. #endif
  191.  
  192. /* This sets all the defaults in a unit type definition.  Note that all
  193.    type structures get blasted with zeros initially, so we really only
  194.    need to do default settings of nonzero values, thus the test.  (It
  195.    helps if the compiler is smart enough to remove dead code.) */
  196.  
  197. void
  198. default_unit_type(u)
  199. int u;
  200. {
  201.     int i;
  202.  
  203.     for (i = 0; utypedefns[i].name != NULL; ++i) {
  204.     if (utypedefns[i].intgetter) {
  205.         if (utypedefns[i].dflt != 0)
  206.           TYPEPROP(utypes, u, utypedefns, i, short) = utypedefns[i].dflt;
  207.     } else if (utypedefns[i].strgetter) {
  208.         if (utypedefns[i].dfltstr != NULL)
  209.           TYPEPROP(utypes, u, utypedefns, i, char *) =
  210.         (char *) utypedefns[i].dfltstr;
  211.     } else {
  212.         TYPEPROP(utypes, u, utypedefns, i, Obj *) = lispnil;
  213.     }
  214.     }
  215. }
  216.  
  217. /* This sets all the defaults in a material type definition. */
  218.  
  219. void
  220. default_material_type(m)
  221. int m;
  222. {
  223.     int i;
  224.     
  225.     for (i = 0; mtypedefns[i].name != NULL; ++i) {
  226.     if (mtypedefns[i].intgetter) {
  227.         if (mtypedefns[i].dflt != 0)
  228.           TYPEPROP(mtypes, m, mtypedefns, i, short) = mtypedefns[i].dflt;
  229.     } else if (mtypedefns[i].strgetter) {
  230.         if (mtypedefns[i].dfltstr != NULL)
  231.           TYPEPROP(mtypes, m, mtypedefns, i, char *) = (char *) mtypedefns[i].dfltstr;
  232.     } else {
  233.         TYPEPROP(mtypes, m, mtypedefns, i, Obj *) = lispnil;
  234.     }
  235.     }
  236. }
  237.  
  238. /* This sets all the defaults in a terrain type definition. */
  239.  
  240. void
  241. default_terrain_type(t)
  242. int t;
  243. {
  244.     int i;
  245.     
  246.     for (i = 0; ttypedefns[i].name != NULL; ++i) {
  247.     if (ttypedefns[i].intgetter) {
  248.         if (ttypedefns[i].dflt != 0)
  249.           TYPEPROP(ttypes, t, ttypedefns, i, short) = ttypedefns[i].dflt;
  250.     } else if (ttypedefns[i].strgetter) {
  251.         if (ttypedefns[i].dfltstr != 0)
  252.           TYPEPROP(ttypes, t, ttypedefns, i, char *) = (char *) ttypedefns[i].dfltstr;
  253.     } else {
  254.         TYPEPROP(ttypes, t, ttypedefns, i, Obj *) = lispnil;
  255.     }
  256.     }
  257. }
  258.  
  259. #endif /* SPECIAL */
  260.  
  261. char *
  262. index_type_name(x)
  263. int x;
  264. {
  265.     return ((x) == UTYP ? "unit" : ((x) == MTYP ? "material" : "terrain"));
  266. }
  267.  
  268. /* This function allocates a parameter table and fills it with a default. */
  269.  
  270. void
  271. allocate_table(tbl, reset)
  272. int tbl, reset;
  273. {
  274.     int i, lim1, lim2, dflt = tabledefns[tbl].dflt;
  275.     short *rslt;
  276.     extern int canaddutype, canaddmtype, canaddttype;
  277.  
  278.     if (reset) *(tabledefns[tbl].table) = NULL;
  279.     if (*tabledefns[tbl].table == NULL) {
  280.     lim1 = numtypes_from_index_type(tabledefns[tbl].index1);
  281.     lim2 = numtypes_from_index_type(tabledefns[tbl].index2);
  282.     if (lim1 == 0) {
  283.         run_warning("Can't allocate the %s table, no %s types defined",
  284.             tabledefns[tbl].name, index_type_name(tabledefns[tbl].index1));
  285.         return;
  286.     }
  287.     if (lim2 == 0) {
  288.         run_warning("Can't allocate the %s table, no %s types defined",
  289.             tabledefns[tbl].name, index_type_name(tabledefns[tbl].index2));
  290.         return;
  291.     }
  292.     /* Allocate the table itself. */
  293.     rslt = (short *) xmalloc(lim1 * lim2 * sizeof(short));
  294.     /* Put the table's default everywhere in the table. */
  295.     for (i = 0; i < lim1 * lim2; ++i) rslt[i] = dflt;
  296.     *(tabledefns[tbl].table) = rslt;
  297.     /* For each index, flag that no more types of that sort allowed. */
  298.     switch (tabledefns[tbl].index1) {
  299.       case UTYP: canaddutype = FALSE;  break;
  300.       case MTYP: canaddmtype = FALSE;  break;
  301.       case TTYP: canaddttype = FALSE;  break;
  302.     }
  303.     switch (tabledefns[tbl].index2) {
  304.       case UTYP: canaddutype = FALSE;  break;
  305.       case MTYP: canaddmtype = FALSE;  break;
  306.       case TTYP: canaddttype = FALSE;  break;
  307.     }
  308.     }
  309. }
  310.  
  311. int
  312. numtypes_from_index_type(x)
  313. int x;
  314. {
  315.     return ((x) == UTYP ? numutypes : ((x) == MTYP ? nummtypes : numttypes));
  316. }
  317.